﻿using System;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections.Generic;

using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.Model;
using HsTec.Mobile.Green.Util;
using Baimasoft.Manage.SqlServerDAL;
using System.Data;


public partial class ReportStorage_ExportMonthlyReport : System.Web.UI.Page
{
    EnergyProportionBLL bll = new EnergyProportionBLL();
    private string sEditStatus = String.Empty;
    Param MyP = new Param();
    List<EnergyProportion> list = new List<EnergyProportion>();
    private string sID = String.Empty;
    private string sArea = String.Empty;
    private string sYears = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_Pers_ID"] == null)
        {
            Response.Redirect("../Logon.aspx");
        }
        if (!IsPostBack)
        {
            hidEdit.Value = "0";
        }
    }
    protected void SaveData_ServerClick(object sender, EventArgs e)
    {
        if (sltMonth.SelectedValue == "")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择年月！');</script>");
            return;
        }
        if (bll.Exists("Area_ID='-1' and Years='" + sltYear.Value.Trim() + sltMonth.SelectedValue.Trim() + "'"))
        {
            hidEdit.Value = "1";
        }
        string insertList = "Powers,Powers_1,Powers_1_1,Powers_1_2,Powers_1_2_1,Powers_1_3,Powers_2,Powers_2_1,Powers_3,Powers_3_1,Powers_3_1_1,Powers_3_1_2,Powers_3_1_3,Powers_3_2,Gas,Gas_1,Gas_2,DieselFuel,DieselFuel_1,DieselFuel_2";
        string[] ary = insertList.Split(',');
        list.Clear();
        for (int i = 0; i < ary.Length; i++)
        {
            EnergyProportion model = new EnergyProportion();
            model.Area_ID = int.Parse("-1");
            model.Years = sltYear.Value.Trim() + sltMonth.SelectedValue.Trim();
            model.AddDate = DateTime.Now;
            model.UpdateDate = DateTime.Now;
            model.UpdateUser = -1;
            model.Subentry = ary[i];
            TextBox tb = (TextBox)this.FindControl("txt" + ary[i]);
            model.Proportion = Decimal.Parse(tb.Text.Trim());
            model.IsCalculat = "1";
            list.Add(model);
        }
        if (hidEdit.Value == "0")
        {
            if (bll.Exists("Area_ID='-1' and Years='" + sltYear.Value.Trim() + sltMonth.SelectedValue.Trim() + "'"))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('该年月存在，请修改区域或者年月！');</script>");
                return;
            }
            int iBack = bll.Add(list);
            if (iBack == 1)
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功！');location.href='ExportMonthlyReport.aspx'</script>");
            else
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加失败！');</script>");
        }
        else if (hidEdit.Value == "1")
        {
            if (bll.Update(list, sArea, sYears) == 1)
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功！');location.href='ExportMonthlyReport.aspx'</script>");
            else
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败！');</script>");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('传参错误！');</script>");
        }
    }
    protected void sltMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (sltMonth.SelectedValue != "")
        {
            string insertList = "Powers,Powers_1,Powers_1_1,Powers_1_2,Powers_1_2_1,Powers_1_3,Powers_2,Powers_2_1,Powers_3,Powers_3_1,Powers_3_1_1,Powers_3_1_2,Powers_3_1_3,Powers_3_2,Gas,Gas_1,Gas_2,DieselFuel,DieselFuel_1,DieselFuel_2";
            if (bll.Exists("Area_ID='-1' and Years='" + sltYear.Value.Trim() + sltMonth.SelectedValue.Trim() + "'"))
            {
                hidEdit.Value = "1";
                DataView dvPro = SQLHelper.ExecuteDataView("select Subentry,Proportion from tb_EnergyProportion where Area_ID='-1' and Subentry!='Powers' and Years='" + sltYear.Value + sltMonth.SelectedValue + "'");
                for (int i = 0; i < dvPro.Count; i++)
                {
                    TextBox tb = (TextBox)this.FindControl("txt" + dvPro[i]["Subentry"].ToString());
                    try
                    {
                        tb.Text = dvPro[i]["Proportion"].ToString();
                    }
                    catch
                    {
                        tb.Text = "";
                    }
                }
            }
            else
            {
                hidEdit.Value = "0";
                string[] ary = insertList.Split(',');
                string slt = String.Empty;
                for (int i = 0; i < ary.Length; i++)
                {
                    slt += ",SUM(" + ary[i] + ") as " + ary[i];
                }
                DataView mydv = SQLHelper.ExecuteDataView("select Years" + slt + " from tb_ReportAnalysis where Years='" + sltYear.Value + sltMonth.SelectedValue + "' and  Area_id!='41' group by Years");
                if (mydv.Count == 1)
                {
                    for (int i = 1; i < ary.Length; i++)
                    {
                        Label tb = (Label)this.FindControl("lab" + ary[i]);
                        double per = 0.00;
                        try
                        {
                            per = Double.Parse(mydv[0][ary[i]].ToString()) / Double.Parse(mydv[0][ary[i].Substring(0, ary[i].LastIndexOf('_'))].ToString()) * 100;
                        }
                        catch
                        {
                            per = 0.00;
                        }
                        tb.Text = per.ToString("0.00") + "%";
                        TextBox tbx = (TextBox)this.FindControl("txt" + ary[i]);
                        if (tbx.Enabled)
                            tbx.Text = "";
                    }
                }
                else
                {
                    for (int i = 1; i < ary.Length; i++)
                    {
                        Label tb = (Label)this.FindControl("lab" + ary[i]);
                        tb.Text = "";
                        TextBox tbx = (TextBox)this.FindControl("txt" + ary[i]);
                        if (tbx.Enabled)
                            tbx.Text = "";
                    }
                }
            }
        }
        else
        {
            hidEdit.Value = "0";
        }
    }
}
